package org.shan.hangtian.server.session;


import org.shan.hangtian.tp.RunMode;

import io.netty.channel.Channel;

/**
 * Created by amanda.shan on 2020/11/6.
 */
public class SessionManager {

    public static SessionManager INSTANCE = new SessionManager();

    public static SessionManager getInstance(){
        return INSTANCE;
    }

    private Channel onlineChannel;

    private SessionLis sessionLis;

    public void setSessionLis(SessionLis sessionLis) {
        this.sessionLis = sessionLis;
    }

    public synchronized void refreshChannel(Channel channel){
        this.onlineChannel = channel;
        if (sessionLis != null){
            sessionLis.online();
        }
    }

    public synchronized void removehChannel(Channel channel){

        if (channel.equals(onlineChannel)){
            this.onlineChannel = null;
        }

        if (sessionLis != null){
            if (onlineChannel == null){
                sessionLis.offline();
            }else{
                sessionLis.online();
            }
        }
    }

    public void modeChange(RunMode runMode){
        if (sessionLis != null){
            sessionLis.modeChange(runMode);
        }
    }

    public boolean isOnline(){
        return onlineChannel != null;
    }

    public Channel getOnlineChannel() {
        return onlineChannel;
    }

    public static interface SessionLis{
        void  online();

        void offline();

        default  void modeChange(RunMode runMode){

        }
    }
}
